<?php
/**
 * Created by PhpStorm.
 * User: fzf
 * Date: 19-4-25
 * Time: 下午5:09
 */
namespace app\common\wechat;

use app\common\model\WechatAutoreply;
use app\common\model\Wx;
use EasyWeChat\Kernel\Exceptions\HttpException;
use EasyWeChat\Factory;
use app\common\model\WechatResponse;
use EasyWeChat\Kernel\Messages\MiniProgramPage;

/**
 * Class MuWechat  多微信号管理
 * @package app\common\wechat
 */
class MuWechat
{

    public $user;
    public $app;
    //public $res;

    protected $methods=[
        'getText',
        'getImage',
        'getVoice',
        'getVideo',
        'getLocation',
        'getLink',
        'getFile',
        'onClick',
        'onView',
        'onsubscribe',
        'onUnsubscribe',
        'onLocation'

    ];

    function getRes($eventkey){

        $ret=WechatResponse::get(['eventkey'=>$eventkey,'status'=>'normal']);
        if($ret){
            $ret->content= json_decode($ret->content,true);
            return $ret;
        }

        return false;
    }

    //执行资源里定义的工作
    function queryRes($eventkey,$message){
        $res=$this->getRes($eventkey);
        //print_r($message);
        if($res){
            if($res['type']=='text'){
                //自动回复
                return $res['content']['content'];
            }else{

                $service=$this->app->customer_service;
                //$msg='<a  href="http://www.qq.com"  data-miniprogram-appid="wx57b669f10b6cf56c"  data-miniprogram-path="pages/index/index">  点击跳小程序</a>';

                $msg=new MiniProgramPage(['thumb_media_id'=>'', 'appid'=>'wx57b669f10b6cf56c', 'pagepath'=>'pages/index/index',]);
                $service->message($msg)->to($message['FromUserName'])->send();
                //return 'app';
                return '1';
            }

        }
    }

    function  __construct($token)
    {
        $user=Wx::get(['token'=>$token]);
        if($user){
            $config=config('wechat');
            $config['app_id']=$user->appid;
            $config['secret']=$user->secret;
            $config['token']=$user->wxtoken;
            $config['aeskey']=$user->aeskey;
            $this->app= Factory::officialAccount($config);
            $this->user=$user;
        }else{
            throw new HttpException('初始化失败');
        }
    }


    function handle(){
        try{
            $this->app->server->push(function($message){
                $type=strtolower($message['MsgType']);
                if($type=='event'){
                    $event=strtolower($message['Event']);
                    $method="on{$event}";
                }else{
                    $method="get{$type}";
                }
                return call_user_func_array([$this,$method],['message'=>$message]);

            });
            $response = $this->app->server->serve();
            return $response->send();
        }catch (\Exception $e){
            return $e->getMessage();
        }
    }

    //菜单点击事件消息
    function onclick($message){
        $eventKey=empty($message['EventKey'])?null:strtolower($message['EventKey']);
        $res=null;
        if($eventKey){
            return $this->queryRes($eventKey,$message);
        }
        return '未知菜单';
    }

    function getText($message){
        $content=$message['Content'];
        //自动回复
        $autoreply=WechatAutoreply::get(['text'=>$content, 'status' => 'normal']);
        if($autoreply){
            return $this->queryRes($autoreply->eventkey,$message);
        }
    }

    function __call($name, $arguments)
    {
        // TODO: Implement __call() method.
        return '未知的信息类型';
    }

}
